﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NRadioTray.App.Domain;

namespace NRadioTray.App.Options
{
    public partial class ItemView : Form
    {
        public ItemView()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.nradiotray_ico;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (this.Radio == null)
            {
                return;
            }
            this.txtName.Text = this.Radio.Name;
            this.txtUrl.Text = this.Radio.Url;

        }

        public Radio Radio { get; set; }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.errorProvider1.Clear();

            if (this.ValidateRadio())
            {
                if (this.Radio == null)
                {
                    this.Radio = new Domain.Radio();
                }

                this.Radio.Name = this.txtName.Text;
                this.Radio.Url = this.txtUrl.Text;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private bool ValidateRadio()
        {
            bool validation = true;

            if (this.txtName.Text == string.Empty)
            {
                this.errorProvider1.SetError(this.txtName, "Name is empty!");
                validation &= false;
            }

            if (this.txtUrl.Text == string.Empty)
            {
                this.errorProvider1.SetError(this.txtUrl, "Url is empty!");
                validation &= false;
            }

            return validation;
        }
    }
}
